<!DOCTYPE HTML>
<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>
<script>
function testImageBitmap(source)
{
    return createImageBitmap(source, 0, 0, 0x8000, 0x8000, {imageOrientation: "flipY", premultiplyAlpha: "none"}).then(function() {
        assert_true(false, "Promise should be rejected if the cropRect is too big.");
    }, function() {
        return createImageBitmap(source, 5, 5, 10, 10, {resizeWidth: 0x8000, resizeHeight: 0x8000}).then(function() {
            assert_true(false, "Promise should be rejected if the resizeWidth or resizeHeight is too big.");
        }, function() {
        });
    });
}

function initializeTestCanvas()
{
    var testCanvas = document.createElement("canvas");
    testCanvas.width = 20;
    testCanvas.height = 20;
    var testCtx = testCanvas.getContext("2d");
    testCtx.fillStyle = "rgb(255, 0, 0)";
    testCtx.fillRect(0, 0, 10, 10);
    testCtx.fillStyle = "rgb(0, 255, 0)";
    testCtx.fillRect(10, 0, 10, 10);
    testCtx.fillStyle = "rgb(0, 0, 255)";
    testCtx.fillRect(0, 10, 10, 10);
    testCtx.fillStyle = "rgb(0, 0, 0)";
    testCtx.fillRect(10, 10, 10, 10);
    return testCanvas;
}

function getBlobWithXhr(url)
{
    return new Promise((resolve, reject) => {
        var xhr = new XMLHttpRequest();
        xhr.open("GET", url);
        xhr.responseType = 'blob';
        xhr.send();
        xhr.onload = function() {
            resolve(xhr.response);
        };
    });
}

function loadImage(url)
{
    return new Promise((resolve, reject) => {
        var image = new Image();
        image.onload = function() {
            resolve(image);
        }
        image.src = url;
    });
}

function loadVideo(url)
{
    return new Promise((resolve, reject) => {
        var video = document.createElement("video");
        video.oncanplaythrough = function() {
            resolve(video);
        };
        video.src = url;
    });
}

// Blob
promise_test(function() {
    return getBlobWithXhr('../canvas/resources/pattern.png').then(testImageBitmap);
}, 'createImageBitmap from a Blob with a big cropRect.');

// HTMLCanvasElement
promise_test(function() {
    var testCanvas = initializeTestCanvas();
    return testImageBitmap(testCanvas);
}, 'createImageBitmap from a HTMLCanvasElement with a big cropRect.');

// HTMLImageElement
promise_test(function() {
    return loadImage('../canvas/resources/pattern.png').then(testImageBitmap);
}, 'createImageBitmap from a HTMLImageElement with a big cropRect.');

// ImageBitmap
promise_test(function() {
    var testCanvas = initializeTestCanvas();
    return createImageBitmap(testCanvas).then(testImageBitmap);
}, 'createImageBitmap from an ImageBitmap with a big cropRect.');

// ImageData
promise_test(function() {
    var canvas = initializeTestCanvas();
    var ctx = canvas.getContext("2d");
    var data = ctx.getImageData(0, 0, 20, 20);
    return testImageBitmap(data);
}, 'createImageBitmap from an ImageData with a big cropRect.');

// HTMLVideoElement
promise_test(function() {
    return loadVideo('../../compositing/resources/video.ogv').then(testImageBitmap);
}, 'createImageBitmap from a HTMLVideoElement with resize option.');
</script>
